import express from 'express'
import * as artices from '../../controllers/articles.js'
import * as category from '../../controllers/category.js'

const router = express.Router();

//删除文章类别
router.post("/delete", async (req, res) => {
    let id = req.body?.id ?? "";
    if (id == "") {
        res.send({
            code: 400,
            msg: "参数错误"
        });
        return;
    }
    //方式1：用状态判断=>只能控制设计时确定的默认分类不被删除，无法保证在用的分类不被删除
    //1.基于id查询分类，判断分类是否可以删除
    // let [cate] = await category.selectCategoryById(id);
    // if (cate.state == 1) {
    //     res.send({
    //         code: 200,
    //         msg: "默认分类不能删除"
    //     })
    //     return;
    // }
    //方式2：用数据库关联方式判断
    let list = await artices.selectArticleById(id);
    if (list.length != 0) {
        res.send({
            code: 200,
            msg: "分类关联文章，不能删除"
        })
        return;
    };
    //2.在可以删除的情况下，删除分类
    await category.deleteCategoryById(id);
    res.send({
        code: 200,
        msg: "删除成功"
    })
});
//新增文章类别
router.post("/add", async (req, res) => {
    let name = req.body?.name ?? "";
    let slug = req.body?.slug ?? "";
    if (name == "" || slug == "") {
        res.send({
            code: 400,
            msg: "参数错误"
        });
        return;
    }
    let list1 = await category.selectCategoryByName(name);
    if (list1.length != 0) {
        res.send({
            code: 400,
            msg: "分类名称已存在"
        });
        return;
    }
    let list2 = await category.selectCategoryBySlug(slug);
    if (list2.length != 0) {
        res.send({
            code: 400,
            msg: "分类别名已存在"
        });
        return;
    }

    await category.addNewCategory(name, slug);

    res.send({
        code: 200,
        msg: "添加"
    });
});
//编辑文章类别
router.post("/edit", async (req, res) => {

    let id = req.body?.id ?? "";
    let name = req.body?.name ?? "";
    let slug = req.body?.slug ?? "";
    if (id == "" || name == "" || slug == "") {
        res.send({
            code: 400,
            msg: "参数错误"
        });
        // 功能1 为最近的方法提供返回值
        // 功能2 结束最近的方法
        return;
    }

    let list1 = await category.selectCategoryByName(name);
    if (list1.length != 0 && list1[0].id != id) {
        res.send({
            code: 400,
            msg: "分类名称已存在"
        });
        return;
    }
    let list2 = await category.selectCategoryBySlug(slug);
    if (list2.length != 0 && list2[0].id != id) {
        res.send({
            code: 400,
            msg: "分类别名已存在"
        });
        return;
    }

    await category.updateCategory(name, slug, id);

    res.send({
        code: 200,
        msg: "编辑成功"
    });
})

//所有文章类别
router.get("/list", async (req, res) => {
    let data = await category.selectAllCategory();
    res.send({
        code: 200,
        msg: "查询成功",
        data: data
    });
})

//根据id查询指定文章类别
router.get("/search", async (req, res) => {
    let id = req.query?.id ?? "";
    if (id == "") {
        res.send({
            code: 400,
            msg: "文章类别ID参数错误"
        });
        return;
    }
    let data = await category.selectCategoryById(id);
    res.send({
        code: 200,
        msg: "查询成功",
        data: data
    });
})
export default router;